package net.jingkao.app.common;

import org.steven0lisa.typhoon.R;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.webkit.ConsoleMessage;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;

/**
 * 基础ChromeClient，提供便捷的控制方法
 * 
 * @author chao
 * 
 */
public class CommonWebChromeClient extends WebChromeClient {

	static final Logger log = Logger.getLog(CommonWebChromeClient.class);

	Context context;

	public CommonWebChromeClient(Context context) {
		this.context = context;
	}

	@Override
	public boolean onConsoleMessage(ConsoleMessage cm) {
		log.d("%s <== %s:%d", cm.message(), cm.sourceId(), cm.lineNumber());
		return true;
	}

	@Override
	public boolean onJsAlert(WebView view, String url, String message,
			JsResult result) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setMessage(message)
				.setNeutralButton("好", new OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int arg1) {
						dialog.dismiss();
					}
				}).show();
		result.cancel();
		return true;
	}

}
